<!-- 登录页面 -->
<template>
  <div class="container">
    <div class="form">
      <!-- 标题部分 -->
      <div class="title">
        <span>千峰后台管理系统</span>
      </div>
      <!-- 表单部分 -->
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="用户名" prop="name">
          <el-input v-model="ruleForm.name"></el-input>
        </el-form-item>
        <el-form-item label="用户名密码" prop="pwd">
          <el-input v-model="ruleForm.pwd"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')"
            >登录</el-button
          >
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
// 引入http请求
import { loginApi } from "@/http/http";
import { setCookie } from "@/utils/cookie.js";
export default {
  data() {
    return {
      ruleForm: {
        name: "",
        pwd: "",
      },
      rules: {
        name: [{ required: true, message: "请输入用户名", trigger: "blur" }],
        pwd: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
    };
  },
  //方法集合
  methods: {
    submitForm(formName) {
      // 登录事件
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // 验证通过，调用登录接口
          loginApi(this.ruleForm).then((res) => {
            console.log(22, res);
            if (res.code == 200) {
              // 登录成功后
              // 存入cookie
              setCookie("token", res.data.token);
              localStorage.setItem("role", res.data.role);
              this.$message({
                message: "登录成功",
                type: "success",
              });
              setTimeout(() => {
                this.$router.push("/");
              }, 1000);
            } else {
              this.$message("账户不存在");
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      // 重置事件
      this.$refs[formName].resetFields();
    },
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {},
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {},
};
</script>
<style scoped lang='less'>
.container {
  width: 100%;
  height: 100%;
  background: url("../assets/1.png") no-repeat center;
  background-size: cover;
  .form {
    width: 370px;
    height: 298px;
    padding: 5px 10px;
    background-color: #fff;
    border-radius: 10px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    .title {
      width: 100%;
      line-height: 50px;
      text-align: center;
      font-size: 18px;
      font-weight: bold;
    }
    /deep/.ivu-form {
      width: 300px;
    }
    /deep/.ivu-btn {
      width: 300px;
      height: 30px;
    }
  }
}
</style>